package com.example.pattern.guide

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import com.example.pattern.R
import com.example.pattern.utils.dp2px


class Indicator(context: Context,attrs:AttributeSet?):View(context, attrs) {
    private var mSelectedWidth = dp2px(40)
    private var mNormalWidth = dp2px(20)
    private var mHeight = dp2px(12)
    private var mSpace = dp2px(6)
    private var mCornerRadius = dp2px(6).toFloat()
    private var mStartX = 0f
    private var mTop = 0f
    private var mCurrentIndex = 2
    private var mSelectedColor = resources.getColor(R.color.main_red, null)
    private var mNormalColor = resources.getColor(R.color.light_dark, null)
    private val mPaint = Paint().apply {
        color = mNormalColor
        style = Paint.Style.FILL
        isAntiAlias = true
    }
    var addPageChangeCallBack:((page:Int)->Unit)? = null
    private var mMoveSpace = 0f
    private var mDirection:Direction = Direction.None//移动方向
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        var m_width = MeasureSpec.getSize(widthMeasureSpec)
        val width_model = MeasureSpec.getMode(widthMeasureSpec)
        if (width_model != MeasureSpec.EXACTLY) {
            m_width = 3 * mNormalWidth + mSelectedWidth + 3 * mSpace
        }
        var m_height = MeasureSpec.getSize(heightMeasureSpec)
        val height_model = MeasureSpec.getMode(heightMeasureSpec)
        if (height_model != MeasureSpec.EXACTLY) {
            m_height = mHeight
        }
        setMeasuredDimension(m_width, m_height)
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        mStartX = (width -(3 * mNormalWidth + mSelectedWidth + 3 * mSpace)).toFloat() /2
        mTop = (height - mHeight).toFloat()/2
    }

    override fun onDraw(canvas: Canvas) {
        for(i in 0 until 4){
            var left = mStartX +if (i>mCurrentIndex){
                mSelectedWidth + mSpace + (i-1)*(mNormalWidth + mSpace)
            }else{
                i*(mNormalWidth+mSpace)
            }
            var right = left + if(i==mCurrentIndex) mSelectedWidth else mNormalWidth
            if(mDirection != Direction.None){
                when (i) {
                    mCurrentIndex -> {//判断是不是当前这个点
                        if (mDirection == Direction.Left) {
                            right -= dp2px(20)
                            right += mMoveSpace //left往右边移动
                        } else {
                            left += dp2px(20)
                            left -= mMoveSpace //往右边移动
                        }
                    }
                    mCurrentIndex-1 ->{  //上一个点
                        if (mDirection == Direction.Right){
                            right  += dp2px(20)
                            right -= mMoveSpace
                        }
                    }
                    mCurrentIndex+1->{ //下一个点
                        if (mDirection == Direction.Left){
                            left -= dp2px(20)
                            left += mMoveSpace
                        }
                    }
                }
            }
            mPaint.color = if (i == mCurrentIndex)mSelectedColor else mNormalColor
            canvas.drawRoundRect(
                left,mTop,right,mTop+mHeight,mCornerRadius,mCornerRadius,mPaint
            )
        }
    }
    fun select(index:Int){
        if(mCurrentIndex == index){
            mDirection = Direction.None
        }else{
            mDirection = if(mCurrentIndex>index)Direction.Left else Direction.Right
            mCurrentIndex = index
            invalidate()
            startMoveAnimation()
        }
    }
    private fun startMoveAnimation(){
        ValueAnimator.ofFloat(0f,dp2px(20).toFloat()).apply {
            duration = 500
            addUpdateListener {
                mMoveSpace = it.animatedValue as Float
                invalidate()
            }
            start()
        }
    }
    override fun onTouchEvent(event: MotionEvent?): Boolean {
        if(event?.action == MotionEvent.ACTION_DOWN) {
            var index = 0
            if (event.x > width / 2) {
                index = mCurrentIndex + 1
                if (index > 3) {
                    return true
                }

            } else {
                index = mCurrentIndex - 1
                if (index < 0) {
                    return true
                }
            }
            addPageChangeCallBack?.let { it(index) }
        }
        return true
    }
}
enum class Direction{
    Left,Right,None
}
